﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Windows;
using JasLib.Extensions;

namespace JasLib.Wpf
{
    [MarkupExtensionReturnType(typeof(object))]
    public class SelfExtension : MarkupExtension
    {
        public sealed override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }

            IProvideValueTarget provideValueTarget = serviceProvider.GetService<IProvideValueTarget>();
            if (provideValueTarget != null)
            {
                return provideValueTarget.TargetObject;
            }

            throw new InvalidOperationException("Unable to find self!");
        }
    }

    [MarkupExtensionReturnType(typeof(object))]
    public abstract class ThisExtension : MarkupExtension
    {
        public sealed override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    [MarkupExtensionReturnType(typeof(TimeSpan))]
    public class TimeSpanExtension : MarkupExtension
    {
        private TimeUnit _timeUnit = TimeUnit.Seconds;
        public TimeUnit TimeUnit
        {
            get { return _timeUnit; }
            set { _timeUnit = value; }
        }

        private double _quantity = 0.0;
        public double Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return GeneralUtilities.TimeUnitsToTimeSpan(_quantity, _timeUnit);
        }
    }
}
